using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ResourceBlender_Express.Properties;
using System.Data.SQLite;
using System.IO;

namespace ResourceBlender_Express {
    /// <summary>
    /// The first time setup wizard which allows a user to set up a database
    /// </summary>
    public partial class WinInstaller : Window {
        private DatabaseSelector databaseSelector;

        /// <summary>
        /// Initializes a new <see cref="WinInstaller"/>.
        /// </summary>
        public WinInstaller() {
            InitializeComponent();
            Settings.Default.DBPath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ResourceBlender"), "ResourceBlender.db3");
            databaseSelector = new DatabaseSelector(); // for design time support
            tbDb.Content = databaseSelector;
        }

        /// <summary>
        /// Moves to the previous page.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        protected void btnPrevious_Click(object sender, RoutedEventArgs e) {
            switch((tabWizard.SelectedItem as TabItem).Name.ToLower()) {
                case "tbdb":
                    tbStart.IsSelected = true;
                    btnPrevious.Visibility = Visibility.Hidden;
                    break;
                case "tbfinish":
                    tbDb.IsSelected = true;
                    btnPrevious.Visibility = Visibility.Visible;
                    break;
            }
            btnNext.Content = "_Next";
        }

        /// <summary>
        /// Moves to the next page or finishes the wizard if the last page is active.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        protected void btnNext_Click(object sender, RoutedEventArgs e) {
            switch((tabWizard.SelectedItem as TabItem).Name.ToLower()) {
                case "tbstart":
                    if(radRbt.IsChecked == true) {
                        Settings.Default.FirstRun = false;
                        Settings.Default.Save();
                        DialogResult = false;
                        Close();
                    } else if(radDb.IsChecked == true) {
                        tbDb.IsSelected = true;
                        btnNext.Content = "_Next";
                        btnPrevious.Visibility = Visibility.Visible;
                    }
                    break;
                case "tbdb":
                    if(databaseSelector.ValidateSettings()) {
                        tbFinish.IsSelected = true;
                        btnNext.Content = "_Finish";
                        btnPrevious.Visibility = Visibility.Visible;
                    }
                    break;
                case "tbfinish":
                    Settings.Default.FirstRun = false;
                    Settings.Default.Save();
                    Database db = null;
                    try {
                        db = new Database();
                        db.OpenConnection();
                        db.AddDummyData();
                    } catch(Exception ex) {
                        DialogBox.ShowAlert(this, "Error adding sample application: ", ex.Message);
                    } finally {
                        if(db != null && db.Connected) {
                            try {
                                db.CloseConnection();
                            } catch { }
                        }
                    }
                    this.DialogResult = true;
                    Close();
                    break;
            }
        }
        
        /// <summary>
        /// Opens a URL
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Navigation.RequestNavigateEventArgs"/> instance containing the event data.</param>
        private void lnkWiki_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) {
            System.Diagnostics.Process.Start(e.Uri.ToString());
        }

        /// <summary>
        /// Opens the help file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Navigation.RequestNavigateEventArgs"/> instance containing the event data.</param>
        private void lnkHelpFile_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) {
            Utils.OpenHelp(null);
        }
    }
}
